import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:github_client_app/db/user_service.dart';
import 'package:github_client_app/models/user.dart';

class Home1Page extends StatefulWidget {
  const Home1Page({Key? key}) : super(key: key);

  @override
  _Home1PageState createState() => _Home1PageState();
}

class _Home1PageState extends State<Home1Page> {
  // final future = Future.delayed(Duration(seconds: 1), () => 'nibao');
  // final stream = Stream.periodic(Duration(seconds: 1), (_) {
  //   // print(_);
  //   return 'kkk';
  // });

  final streamCol = StreamController.broadcast();

  @override
  void initState() {
    // future.then((val) => print(val));
    // stream.listen((event) {
    //   print(event);
    // });
    streamCol.stream.listen(
      (event) {
        print(event);
      },
    );
    super.initState();

    UserService userService = UserService();
    User user = User();
    user.age = 18;
    user.name = "李四";
    user.keywords = "guanjianzi";
    // userService.insert(user.toJson());
  }

  Stream<DateTime> getName() async* {
    while (true) {
      await Future.delayed(Duration(seconds: 1), () => 'nibao');
      yield DateTime.now();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('home1'),
      ),
      body: SingleChildScrollView(
        child: Column(
          mainAxisSize: MainAxisSize.max,
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            ElevatedButton(
                onPressed: () {
                  // streamCol.add(1);
                  streamCol.sink.add(1);
                },
                child: Text('1')),
            ElevatedButton(
                onPressed: () {
                  streamCol.add('hi');
                },
                child: Text('hi')),
            ElevatedButton(
                onPressed: () {
                  streamCol.close();
                },
                child: Text('close')),
            StreamBuilder(
              stream: getName(),
              builder: (context, snapshot) {
                switch (snapshot.connectionState) {
                  case ConnectionState.none:
                    return Text("没有stream");
                  case ConnectionState.waiting:
                    return Text("等待stream");
                  case ConnectionState.active:
                    if (snapshot.hasData) {
                      return Text("data:${snapshot.data}");
                    } else {
                      return Text("error:${snapshot.error}");
                    }
                  case ConnectionState.done:
                    return Text("stream已经关闭");
                }
              },
            ),
            ElevatedButton(
                onPressed: () {
                  Future(
                    () => 'future1',
                  ).then((value) => print(value));
                  Future(
                    () => 'future2',
                  ).then((value) => print(value));

                  // setState(() {
                  //   print("object");
                  // });
                  // print("object33");
                  // Future.delayed(Duration(seconds: 2), () => print('future2'));
                  // Future.delayed(Duration(seconds: 1), () => print('future1'));
                  Future.value("value1").then((value) => print(value));
                  Future.value("value2").then((value) => print(value));

                  print("object55");
                },
                child: Text("home")),
            SizedBox(
              width: 300,
              height: 300,
              child: Stack(
                fit: StackFit.loose,
                alignment: AlignmentDirectional.center,
                children: [
                  Container(
                    width: 200,
                    height: 200,
                    color: Colors.red,
                  ),
                  Container(
                    width: 100,
                    height: 100,
                    color: Colors.blue,
                  ),
                ],
              ),
            ),
            const FlutterLogo(
              textColor: Colors.red,
              style: FlutterLogoStyle.horizontal,
              size: 100,
              curve: Curves.easeOutCubic,
            ),
            const SizedBox(
              height: 200,
              width: 200,
              child: Placeholder(
                color: Colors.blue,
                // fallbackHeight: 400,
                // fallbackWidth: 400,
                strokeWidth: 20,
                child: Text("data"),
              ),
            ),
            Container(
              alignment: Alignment.center,
              color: Colors.orange,
              padding: EdgeInsets.all(20),
              margin: EdgeInsets.all(32),
              // width: 200,
              // height: 200,
              child: Container(
                  foregroundDecoration: BoxDecoration(
                    gradient: LinearGradient(
                        colors: [Colors.yellow, Colors.red],
                        end: Alignment.bottomCenter),
                  ),
                  clipBehavior: Clip.none,
                  // transform: Matrix4._decomposeM,
                  alignment: Alignment(-1, -0.5),
                  color: Colors.teal,
                  width: 100,
                  height: 100,
                  child: Container(
                    width: 50,
                    height: 50,
                    color: Colors.black,
                  )),
            ),
            SizedBox(
              width: double.infinity,
              height: 400,
              child: Wrap(
                // spacing: 80,
                runSpacing: 70,
                // runAlignment: WrapAlignment.end,
                // alignment: WrapAlignment.spaceBetween,
                direction: Axis.vertical,
                // crossAxisAlignment: WrapCrossAlignment.end,
                children: [
                  FlutterLogo(
                    size: 90,
                  ),
                  FlutterLogo(
                    size: 90,
                  ),
                  FlutterLogo(
                    size: 90,
                  ),
                  FlutterLogo(
                    size: 90,
                  ),
                  FlutterLogo(
                    size: 90,
                  ),
                  FlutterLogo(
                    size: 90,
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}
